package engine.control 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.com.control.button.NxButton;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.tab.NxTab;
	import newx.com.control.text.NxLabel;
	/**
	 * ...
	 * @author WangHaiBo  www.woniu.com
	 */
	public class TurnPapersViewGroup 
	{
		private var _viewGroupData		:Array;
		private var _viewGroup			:NxViewGroup;
		private var _preBtn				:NxButton;
		private var _nextBtn			:NxButton;



		private var _showFunction		:Function;
		private var _removeFunction		:Function;
		private var _afterFunction		:Function;
		private var _pageLabel			:NxLabel;
		
		//一页的最大个数
		private var _itemMax				:int =	0;
		//当前页
		private var _nowPage				:int =	1;
		//最大页
		private var _maxPage				:int = 	1;
		
		private var _clickIndex			:int =	0;
		
		private var _isHide				:Boolean;
		private var _chooseIndex			:int =	0;
		/**
		 * viewGroup翻页功能
		 * @param	viewGroup						显示对象
		 * @param	preBtn							前一页按钮
		 * @param	nextBtn							后一页按钮
		 * @param	showFun(mc:movieclip,data:obj)	添加
		 * @param	removeFun(mc:movieclip)			删除
		 * @param	beforFunction(mc:movieclip);
		 * @param	afterFun(mc:movieclip)			翻页后的操作
		 * @param	pageLabel						页码显示
		 * @param   isHide							没有数据是否显示组件
		 */
		public function TurnPapersViewGroup(viewGroup:NxViewGroup, preBtn:NxButton , nextBtn:NxButton,
											showFun:Function,removeFun:Function,afterFun:Function = null,pageLabel:NxLabel = null,isHide:Boolean =true) 
		{
			_viewGroupData	=	[];
			_viewGroup		=	viewGroup;
			_preBtn			=	preBtn;
			_nextBtn		=	nextBtn;
			_showFunction	=	showFun;
			_removeFunction	=	removeFun;
			_afterFunction	=	afterFun;
			_pageLabel		=	pageLabel;
			_isHide			=  	isHide;
			_itemMax		=	_viewGroup.row * _viewGroup.col;
			checkMaxPage();
		}
		/**
		 * 事件侦听
		 */
		public function addListener():void
		{
			_preBtn		.addEventListener(MouseEvent.CLICK, clickHandle);
			_nextBtn	.addEventListener(MouseEvent.CLICK, clickHandle);
		}
		/**
		 * 关闭事件
		 */
		public function removeListener():void
		{
			_preBtn		.removeEventListener(MouseEvent.CLICK, clickHandle);
			_nextBtn	.removeEventListener(MouseEvent.CLICK, clickHandle);
		}
		/**
		 * 添加显示数据
		 * @param	val
		 */
		public function addItem(val:Object):void
		{
			var _isHave:Boolean	=	false;
			for(var i:int =0; i < _viewGroupData.length; i++)
			{
				if(_viewGroupData[i] == val)
				{
					_isHave	=	true;
					_viewGroupData[i] = val;
				}
			}
			if(!_isHave)
			{
				_viewGroupData.push(val);
			}
			
			showItem();
			checkMaxPage()
		}
		/**
		 * 获取数据
		 */
		public function get dataProvider():Array
		{
			return _viewGroupData
		}
		/**
		 * 设定数据,从第一页开始
		 */
		public function set dataProvider(val:Array):void
		{
			clear();
			if(val == null)return;
			_viewGroupData	=	val;
			//for (var i:int = 0; i < val.length; i++) 
			//{
				//_viewGroupData.push(val[i]);
			//}
			showItem();
			checkMaxPage()
		}
		/**
		 * 设定数据,保存当前页数
		 */
		public function set dataProviderPage(val:Array):void
		{
//			clear();
			if(val == null)return;
			_viewGroupData	=	val;
			//for (var i:int = 0; i < val.length; i++) 
			//{
			//_viewGroupData.push(val[i]);
			//}
			showItem();
			checkMaxPage()
		}
		/**
		 * 当前页数 
		 * @return 
		 * 
		 */		
		public function get nowPage():int
		{
			return _nowPage;
		}
		public function clear():void
		{
			_viewGroupData	=	[];
			_nowPage		=	1;
			showItem();
			checkMaxPage()
			
		}
		// 不清理当前页数
		public function Pageclear():void
		{
			_viewGroupData	=	[];
//			_nowPage		=	1;
			showItem();
			checkMaxPage()
			
		}
		/**
		 * 设定选择的数据中的指定项
		 * 可自动翻页，定位 
		 * @param val
		 * 
		 */		
		public function set chooseDataItem(val:int):void
		{
			if(_viewGroupData.length != 0)
			{
				_chooseIndex	=	val;
				var index:int	=	0;
				_nowPage	=	Math.floor(_chooseIndex / _itemMax);
				index		=	val % _itemMax;
				if(index >= 0)
				{
					_nowPage ++;
				}
				_clickIndex	=	index;
				showItem();
				checkBtnEnable();
			}
		}
		/**
		 * 设定当前所选项目的索引 
		 * @param val
		 * 
		 */		
		public function set chooseIndex(val:int):void
		{
			_chooseIndex	=	val;
		}
		/**
		 * 获取所要显示的索引
		 * @return 
		 * 当为-1时，表示什么也不显示也不选择
		 * 
		 */		
		public function get chooseIndex():int
		{
			var _checkPage:int	=	Math.floor(_chooseIndex / _itemMax);
			_clickIndex		=	_chooseIndex % _itemMax;
			if(_clickIndex >= 0)
			{
				_checkPage ++;
			}
			if(_checkPage != _nowPage)
			{
				_clickIndex	=	-1;
			}
			return _clickIndex;
		}
		/**
		 * 按钮事件
		 * @param	e
		 */
		private function clickHandle(e:MouseEvent):void
		{
			switch (e.target) 
			{
				case _preBtn:
					_nowPage--
					break;
				case _nextBtn:
					_nowPage++
					break;
				default:
			}
			_clickIndex	=	0
			showItem();
			checkBtnEnable();
		}
		/**
		 * 显示对象
		 */
		private function showItem(isBack:Boolean = true):void
		{
			clearViewGroup()
			var item:MovieClip;
			var itemData:Object	=	null
			var pageIndex:int	=	0;
			for (var i:int = 0; i < _itemMax; i++)
			{
				
				pageIndex	=	(_nowPage - 1)*_itemMax + i
				itemData	=	_viewGroupData[pageIndex];
				item		=	_viewGroup.getItemByIndex(i);
				if (itemData != null)
				{
					item["pageIndex"]	=	pageIndex;
					_showFunction(item,itemData);
				}
				if(!_isHide)
					item.visible = (itemData==null)?false:true;
				
			}
			if (_afterFunction != null && isBack) _afterFunction();
		}
		/**
		 * 清空
		 */
		private function clearViewGroup():void
		{
			var item:MovieClip;
			for (var i:int = 0; i < _itemMax; i++) 
			{
				item				=	_viewGroup.getItemByIndex(i);
				item["pageIndex"]	=	0;
				_removeFunction(item);
			}
		}
		/**
		 * 当前最大的页码
		 */
		private function checkMaxPage():void
		{
			if (_viewGroupData.length % _itemMax > 0) 
			{
				_maxPage =	int(_viewGroupData.length / _itemMax)  + 1;
			}
			else
			{
				_maxPage =	_viewGroupData.length / _itemMax;
			}
			checkBtnEnable();
		}
		/**
		 *  按钮状态
		 */
		private function checkBtnEnable():void
		{
			_preBtn.enabled		=	false;
			_nextBtn.enabled	=	false;
			if (_nowPage >1)
			{
				_preBtn.enabled		=	true;
			}
			if (_nowPage < _maxPage) 
			{
				_nextBtn.enabled	=	true;
			}
			showPage();
		}
		/**
		 * 显示页码
		 */
		private function showPage():void
		{
			if(_maxPage == 0)
			{
				_maxPage = 1;
			}
			if (_pageLabel != null)
			{
				_pageLabel.text	=	String(_nowPage) + "/" + String(_maxPage);
			}
		}


	}

}